﻿#region GNU GPL Version 3 License

/// Copyright 2013 Shailesh Lolam
/// 
/// This file Extenstion.cs is part of AzEzAuto.
/// 
/// AzEzAuto is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
/// 
/// AzEzAuto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
/// 
/// You should have received a copy of the GNU General Public License along with AzEzAuto. If not, see http://www.gnu.org/licenses/.
/// 
/// History:
/// ______________________________________________________________
/// Created         04-2013             Shailesh Lolam

#endregion
      
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.WindowsAzure.Storage.Queue;
using System.IO;
using System.Xml.Serialization;

namespace AzEzAuto.Utils
{
    public static class Extensions
    {
        /*
        public static void PutValue(this CloudQueueMessage message, object value)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                XmlSerializer s = new XmlSerializer(value.GetType());
                s.Serialize(ms, value);
                message.SetMessageContent(ms.ToArray());
            }
        }

        public static T GetValue<T>(this CloudQueueMessage message)
        {
            using (MemoryStream ms = new MemoryStream(message.AsBytes))
            {
                XmlSerializer s = new XmlSerializer(typeof(T));
                return (T)s.Deserialize(ms);
            }
        }
        */

        public static void PutValue(this CloudQueueMessage message, object value)
        {
            using (StringWriter sw = new StringWriter())
            {
                XmlSerializer s = new XmlSerializer(value.GetType());
                s.Serialize(sw, value);
                message.SetMessageContent(sw.ToString());
            }
        }

        public static T GetValue<T>(this CloudQueueMessage message)
        {
            using (StringReader ms = new StringReader(message.AsString))
            {
                XmlSerializer s = new XmlSerializer(typeof(T));
                return (T)s.Deserialize(ms);
            }
        }
    }
}

